package com.jediflasher.game_server.data

import collection.mutable.HashMap
import com.jediflasher.game_server.util.log.Logger
import com.jediflasher.game_server.core.events.EventDispatcher
import com.jediflasher.game_server.events.data.world.{WorldDataEventType, WorldDataEvent}
import java.sql.ResultSet
import collection.immutable.HashSet

/**
 * User: Obi
 * Date: 05.08.11
 * Time: 17:47
 */

class WorldData extends EventDispatcher {

	private val usersHash: HashMap[Int, UserData] = new HashMap[Int, UserData];

	/**
	 * Добавляет нового игрока в мир
	 */
	def addUser(id: Int, resultSet:ResultSet = null) {
		if (id == 0) {
			Logger.logError("wrong user id:"+id)
			return;
		}
		
		if (!usersHash.contains(id)) {
			val user = new UserData(id, resultSet);
			usersHash.put(id, user);
			dispatchEvent(new WorldDataEvent(WorldDataEventType.USER_ADDED, user));
		}
		else {
			Logger.logError("User with id " + id + "already added");
		}
	}

	def getUser(id: Int): UserData = {
		usersHash.get(id).get;
	}

	/**
	 * Удаляет нового игрока из мира
	 */
	def removeUser(id: Int) {
		val user = usersHash.remove(id).get;
		if (user != null) dispatchEvent(new WorldDataEvent(WorldDataEventType.USER_REMOVED, user));
	}

	def getUsers: Iterator[UserData] = {
		usersHash.values.iterator;
	}


}